<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="所属项目" prop="project">
        <el-select
          style="width: 100%;"
          v-model="optionsProjectId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入项目名称"
          :remote-method="getProjectInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in project_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属模块" prop="module">
        <el-select
          style="width: 100%;"
          :disabled="!optionsProjectId.length"
          v-model="optionsModuleId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModuleInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属页面" prop="module_page">
        <el-select
          style="width: 100%;"
          :disabled="!optionsModuleId.length"
          v-model="optionsModulePageId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModulePageInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_page_info"
            :key="index"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="元素标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="标签类型" prop="status">
        <el-select v-model="ruleForm.element_type" placeholder="请选择标签类型" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsElementType"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="标签key" prop="element_key">
        <el-input v-model="ruleForm.element_key"></el-input>
      </el-form-item>

      <el-form-item label="定位数据" prop="content">
        <el-input v-model="ruleForm.content"></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark"></el-input>
      </el-form-item>

      <el-form-item label="元素位置" prop="label_location">
        <el-upload
          class="upload-demo"
          ref="upload"
          :action="action_host"
          multiple
          accept="text/txt"
          :limit="1"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-change="OnChange"
          :file-list="fileList"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="addUpload('label_location')">添加到字段下
          </el-button>
          <el-button style="margin-left: 10px;" size="small" type="danger" @click="delUpload('label_location')">清除字段下文件
          </el-button>
          <span v-if="ruleForm.label_location" style="color: #13ce66">已添加文件</span>
          <span v-else style="color: red">未添加文件</span>
        </el-upload>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'EditLabelElement',
    data() {
      return {
        action_host: this.$http.HOST,
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsElementType: [],

        projects: [], // 项目列表，获取1000条项目数据
        EditUrl: this.$http.LabelElementURL,  // get编辑的URL
        EditIdStr: 'label_element_id',  // 模块id字符串
        pushObjName: 'LabelElement',  // 成功后跳转

        // 其它表数据,搜索到的全部数据
        project_info: [], // 项目列表，获取1000条项目数据
        module_info: [], // 模块列表，获取1000条项目数据
        module_page_info: [], // 页面列表

        // 编辑和添加选中的数据id
        loading: false,
        optionsProjectId: [],
        optionsModuleId: [],
        optionsModulePageId: [],

        // 文件上传
        fileList: [],

        // 默认不是FormData格式
        FormDataStatus: false,

        ruleForm: {
          title: '',
          project: '',
          module: '',
          module_page: '',
          content: '',
          element_key:'',
          element_type:'',
          remark: '',
          // label_location: '',
          status: '',

          project_info: '',
          module_info: '',
          module_page_info: '',

        },  //
        rules: {
          title: [
            {required: true, message: '请输入标题', trigger: 'blur'},
            {min: 1, max: 64, message: '长度在 1 到 64 个字符', trigger: 'blur'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common; // 获取标准CHOICES
      this.optionsElementType = this.getChoices.web.element_type;
      this.$myTool.getObjList(this, this.$http.projectURL, this.projects);  // 获取项目1000条数据
    },
    methods: {

      // 添加到对应的字段下
      addUpload(ruleFormName) {
        this.ruleForm[ruleFormName] = this.fileList[0].raw;
        this.fileList.splice(0, this.fileList.length);
      },
      // 清除字段下文件
      delUpload(ruleFormName) {
        this.ruleForm[ruleFormName] = '';
      },
      // 删除时调用
      handleRemove(file, fileList) {
        this.fileList = fileList
      },
      handlePreview(file) {
        console.log(file);
      },
      // 文件列表发生变化时会更新列表
      OnChange(file, fileList) {
        this.fileList = fileList
      },

      submitForm() {
        this.FormDataStatus = true;
        this.$myTool.submitForm(this)
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this);
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },

      // 获取模块信息
      getModuleInfo(query) {
        let QuerySet = {
          module_project:this.ruleForm.project,
          inputObjName:query
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, QuerySet)
      },

      // 获取页面信息
      getModulePageInfo(query) {
        let QuerySet = {
          module_page_module_id:this.ruleForm.module,
          module_page_project_id:this.ruleForm.project,
          inputObjName:query
        };
        this.$myTool.getObjList(this, this.$http.ModulePageURL, this.module_page_info, QuerySet)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
    // 监听
    watch: {
      'ruleForm.project_info': function (value) {
        if (value) {
          this.optionsProjectId.push(value.id);
          this.project_info.push(value);
        } else {
          this.optionsProjectId = [];
          this.project_info = [];
        }
      },
      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.project = value[0]
      },

      'ruleForm.module_info': function (value) {
        if (value) {
          this.optionsModuleId.push(value.id);
          this.module_info.push(value);
        } else {
          this.optionsModuleId = [];
          this.module_info = [];
        }
      },
      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module = value[0]
      },

      'ruleForm.module_page_info': function (value) {
        if (value) {
          this.optionsModulePageId.push(value.id);
          this.module_page_info.push(value);
        } else {
          this.optionsModulePageId = [];
          this.module_page_info = [];
        }
      },
      optionsModulePageId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module_page = value[0]
      },
    },
    // 计算属性
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },

  }
</script>
